from sqlalchemy import PrimaryKeyConstraint, Sequence
from sqlalchemy.dialects.postgresql import TIMESTAMP

from tzq.extensions import db


class Catalog(db.Model):
    __tablename__ = 'dm_catalog_t'
    __table_args__ = (
        PrimaryKeyConstraint('catalog_id', name='dm_catalog_pk', comment='主键约束'),
        {'comment': '菜单表'},
    )
    catalog_id = db.Column(
        db.BigInteger,
        Sequence('dm_catalog_s'),
        primary_key=True,
        server_default=Sequence('dm_catalog_s').next_value(),
        nullable=False,
        comment='主键ID'
    )
    # 业务字段
    parent_node_name = db.Column(db.String(2000), nullable=False, comment='父级菜单')
    name = db.Column(db.String(2000), nullable=False, comment='菜单')
    description = db.Column(db.String(2000), comment='说明')
    url = db.Column(db.String(2000), nullable=False, comment='url')
    order = db.Column(db.String(2000), nullable=False, comment='节点顺序')
    open_link_type = db.Column(db.String(2000), nullable=False, comment='窗口展示：当前窗口、新窗口')
    # 审计字段
    principal = db.Column(db.String(100), nullable=False, comment='责任人：HW工号')
    online_date = db.Column(TIMESTAMP(timezone=False, precision=0), nullable=False,
                            server_default=db.func.current_date(), comment='上线时间')
    comment = db.Column(db.String(3000), comment='备注')
    delete_flag = db.Column(db.String(10), nullable=False, server_default='N',
                            comment='删除标识：记录该记录是否删除，Y=>删除、N=>未删除')
    created_by = db.Column(db.String(50), nullable=False, server_default='0.0.0.0',
                           comment='创建人：创建人的客户端IP地址')
    creation_date = db.Column(TIMESTAMP(timezone=False, precision=0), nullable=False,
                              server_default=db.func.clock_timestamp(), comment='创建时间')
    last_updated_by = db.Column(db.String(50), nullable=False, server_default='0.0.0.0',
                                comment='最后更新人：更新人的客户端IP地址')
    last_update_date = db.Column(TIMESTAMP(timezone=False, precision=0), nullable=False,
                                 server_default=db.func.clock_timestamp(), comment='最后更新时间')

    def to_dict(self):
        return {
            'catalog_id': self.catalog_id,
            # 业务字段
            'parent_node_name': self.parent_node_name,
            'name': self.name,
            'description': self.description,
            'url': self.url,
            'order': self.order,
            'open_link_type': self.open_link_type,
            # 审计字段
            'principal': self.principal,
            'online_date': self.online_date.strftime('%Y-%m-%d'),
            'comment': self.comment,
            'delete_flag': self.delete_flag,
            'created_by': self.created_by,
            'creation_date': self.creation_date.strftime('%Y-%m-%d %H:%M:%S'),
            'last_updated_by': self.last_updated_by,
            'last_update_date': self.last_update_date.strftime('%Y-%m-%d %H:%M:%S'),
        }


class CatalogModify(db.Model):
    __tablename__ = 'dm_catalog_modify_t'
    __table_args__ = (
        PrimaryKeyConstraint('catalog_modify_id', name='dm_catalog_modify_pk', comment='主键约束'),
        {'comment': '菜单修改表'},
    )
    catalog_modify_id = db.Column(
        db.BigInteger,
        Sequence('dm_catalog_modify_s'),
        primary_key=True,
        server_default=Sequence('dm_catalog_modify_s').next_value(),
        nullable=False,
        comment='主键ID'
    )
    # 业务字段
    event = db.Column(db.String(2000), nullable=False, comment='事情')
    saas_location = db.Column(db.String(2000), nullable=False, comment='菜单在界面的位置')
    operation = db.Column(db.String(2000), nullable=False, comment='操作：修改')
    jalor_location = db.Column(db.String(2000), nullable=False, comment='在公服平台修改')
    # 审计字段
    principal = db.Column(db.String(100), nullable=False, comment='责任人：HW工号')
    online_date = db.Column(TIMESTAMP(timezone=False, precision=0), nullable=False,
                            server_default=db.func.current_date(), comment='上线时间')
    comment = db.Column(db.String(3000), comment='备注')
    delete_flag = db.Column(db.String(10), nullable=False, server_default='N',
                            comment='删除标识：记录该记录是否删除，Y=>删除、N=>未删除')
    created_by = db.Column(db.String(50), nullable=False, server_default='0.0.0.0',
                           comment='创建人：创建人的客户端IP地址')
    creation_date = db.Column(TIMESTAMP(timezone=False, precision=0), nullable=False,
                              server_default=db.func.clock_timestamp(), comment='创建时间')
    last_updated_by = db.Column(db.String(50), nullable=False, server_default='0.0.0.0',
                                comment='最后更新人：更新人的客户端IP地址')
    last_update_date = db.Column(TIMESTAMP(timezone=False, precision=0), nullable=False,
                                 server_default=db.func.clock_timestamp(), comment='最后更新时间')

    def to_dict(self):
        return {
            'catalog_modify_id': self.catalog_modify_id,
            # 业务字段
            'event': self.event,
            'saas_location': self.saas_location,
            'operation': self.operation,
            'jalor_location': self.jalor_location,
            # 审计字段
            'principal': self.principal,
            'online_date': self.online_date.strftime('%Y-%m-%d'),
            'comment': self.comment,
            'delete_flag': self.delete_flag,
            'created_by': self.created_by,
            'creation_date': self.creation_date.strftime('%Y-%m-%d %H:%M:%S'),
            'last_updated_by': self.last_updated_by,
            'last_update_date': self.last_update_date.strftime('%Y-%m-%d %H:%M:%S'),
        }


class CatalogDelete(db.Model):
    __tablename__ = 'dm_catalog_delete_t'
    __table_args__ = (
        PrimaryKeyConstraint('catalog_delete_id', name='dm_catalog_delete_pk', comment='主键约束'),
        {'comment': '菜单删除表'},
    )
    catalog_delete_id = db.Column(
        db.BigInteger,
        Sequence('dm_catalog_delete_s'),
        primary_key=True,
        server_default=Sequence('dm_catalog_delete_s').next_value(),
        nullable=False,
        comment='主键ID'
    )
    # 业务字段
    event = db.Column(db.String(2000), nullable=False, comment='事情')
    saas_location = db.Column(db.String(2000), nullable=False, comment='菜单在界面的位置')
    operation = db.Column(db.String(2000), nullable=False, comment='操作：删除')
    jalor_location = db.Column(db.String(2000), nullable=False, comment='在公服平台删除')
    # 审计字段
    principal = db.Column(db.String(100), nullable=False, comment='责任人：HW工号')
    online_date = db.Column(TIMESTAMP(timezone=False, precision=0), nullable=False,
                            server_default=db.func.current_date(), comment='上线时间')
    comment = db.Column(db.String(3000), comment='备注')
    delete_flag = db.Column(db.String(10), nullable=False, server_default='N',
                            comment='删除标识：记录该记录是否删除，Y=>删除、N=>未删除')
    created_by = db.Column(db.String(50), nullable=False, server_default='0.0.0.0',
                           comment='创建人：创建人的客户端IP地址')
    creation_date = db.Column(TIMESTAMP(timezone=False, precision=0), nullable=False,
                              server_default=db.func.clock_timestamp(), comment='创建时间')
    last_updated_by = db.Column(db.String(50), nullable=False, server_default='0.0.0.0',
                                comment='最后更新人：更新人的客户端IP地址')
    last_update_date = db.Column(TIMESTAMP(timezone=False, precision=0), nullable=False,
                                 server_default=db.func.clock_timestamp(), comment='最后更新时间')

    def to_dict(self):
        return {
            'catalog_delete_id': self.catalog_delete_id,
            # 业务字段
            'event': self.event,
            'saas_location': self.saas_location,
            'operation': self.operation,
            'jalor_location': self.jalor_location,
            # 审计字段
            'principal': self.principal,
            'online_date': self.online_date.strftime('%Y-%m-%d'),
            'comment': self.comment,
            'delete_flag': self.delete_flag,
            'created_by': self.created_by,
            'creation_date': self.creation_date.strftime('%Y-%m-%d %H:%M:%S'),
            'last_updated_by': self.last_updated_by,
            'last_update_date': self.last_update_date.strftime('%Y-%m-%d %H:%M:%S'),
        }
